import { app, ipcMain, Menu, dialog } from 'electron';
import CreatetrayWindow from './tray';
import CreateServiceWindow from './service';
import CreateContentWindow from './content';
import plugInWindow from './plugIn';


let tray;
let service;
let contentWindows = {};
let plugInWindows = {};


ipcMain.on('open-plugin-window', (event, _param) => {
    let param = JSON.parse(_param);
    let windowId = param.name;
    if (plugInWindows.hasOwnProperty(windowId)) {
        const plugInWindow = plugInWindows[windowId];
        plugInWindow.show();
        plugInWindow.focus();
    } else {
        plugInWindows[windowId] = new plugInWindow(param);
        let win = plugInWindows[windowId].win;
        win.on('close', () => {
            win = null;
            delete plugInWindows[windowId];
        });
    };
});

ipcMain.on('open-service-window', () => {
    if (service) {
        service.show();
        service.focus();
    } else {
        service = CreateServiceWindow();
    };
});
ipcMain.on('open-content-window', (event, _param) => {
    let param = JSON.parse(_param);
    let serviceId = param.service;
    let projectId = param.project.projectId;
    let windowId = serviceId + '-' + projectId;
    if (contentWindows.hasOwnProperty(windowId)) {
        const ContentWindow = contentWindows[windowId];
        ContentWindow.show();
        ContentWindow.focus();
    } else {
        contentWindows[windowId] = new CreateContentWindow(param, contentWindows);
        let win = contentWindows[windowId].win;
        win.on('close', () => {
            win = null;
            delete contentWindows[windowId];
            renderTrayMenu();
        });
        renderTrayMenu();
    };
});

ipcMain.on('close-service-window', function () {
    for (const key in contentWindows) {
        if (contentWindows.hasOwnProperty(key)) {
            const element = contentWindows[key];
            let serviceId = key.split('-')[0];
            if (serviceId != 'root') {
                element.win.close();
            };
        }
    };
});



ipcMain.on('get-window-param', (event, winId) => {
    for (const key in contentWindows) {
        if (contentWindows.hasOwnProperty(key)) {
            const element = contentWindows[key];
            if (element.win.id == winId) {
                element.win.webContents.send('set-window-param', JSON.stringify({
                    service: element.service,
                    token: element.token,
                    project: element.project,
                }))
                break;
            };
        }
    };
});
ipcMain.on('updata-window-param', (event, _param) => {
    let param = JSON.parse(_param);
    for (const key in contentWindows) {
        if (contentWindows.hasOwnProperty(key)) {
            const element = contentWindows[key];
            if (element.win.id == param.winId) {
                element.project.title = param.title;
                element.project.description = param.description;
                renderTrayMenu();
                break;
            };
        }
    };
});


let renderTrayMenu = function () {
    var trayMenuArray = [];
    for (const key in contentWindows) {
        if (contentWindows.hasOwnProperty(key)) {
            const element = contentWindows[key];
            var _menuItem = {
                type: 'normal',
                label: element.service + '-' + element.project.title,
                click: function () {
                    element.show();
                    element.focus();
                },
            };
            trayMenuArray.push(_menuItem);
        }
    };
    trayMenuArray.push({
        type: 'separator',
    });
    trayMenuArray.push({
        type: 'normal',
        label: '服务器列表',
        click: function () {
            service.show();
            service.focus();
        },
    });
    trayMenuArray.push({
        type: 'normal',
        label: '退出',
        click: function () {
            tray.destroy();
            app.exit()
        }
    });
    tray.setContextMenu(Menu.buildFromTemplate(trayMenuArray));
};
export default function () {
    tray = CreatetrayWindow()
    service = CreateServiceWindow();
    renderTrayMenu();
    tray.on('click', function () {
        service.show();
        service.focus();
    });
    return service;
}